All files / web/src/app/api/curriculum/[playerId]/sessions/plans/[planId] route.ts

0% Statements 0/354
0% Branches 0/1
0% Functions 0/1
0% Lines 0/354

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import { NextResponse } from 'next/server'
import { eq } from 'drizzle-orm'
import { db } from '@/db'
import { players } from '@/db/schema'
import type { SessionPlan, SlotResult } from '@/db/schema/session-plans'
import { withAuth } from '@/lib/auth/withAuth'
import { canPerformAction, getStudentPresence } from '@/lib/classroom'
import {
  emitSessionEnded,
  emitSessionEndedToPlayer,
  emitSessionStarted,
  emitSessionStartedToPlayer,
} from '@/lib/classroom/socket-emitter'
import {
  acknowledgeGameBreakResults,
  abandonSessionPlan,
  approveSessionPlan,
  completePartTransition,
  completeSessionPlanEarly,
  finishGameBreak,
  getSessionPlan,
  type RedoContext,
  recordRedoResult,
  recordSlotResult,
  startSessionPlan,
  StaleFlowVersionError,
  updateSessionPlanRemoteCamera,
} from '@/lib/curriculum'
import { InvalidFlowTransitionError } from '@/lib/curriculum/session-flow'
import { getUserId } from '@/lib/viewer'

/**
 * Serialize a SessionPlan for JSON response.
 * Converts Date objects to timestamps (milliseconds) for consistent client handling.
 */
function serializePlan(plan: SessionPlan) {
  return {
    ...plan,
    createdAt: plan.createdAt instanceof Date ? plan.createdAt.getTime() : plan.createdAt,
    approvedAt: plan.approvedAt instanceof Date ? plan.approvedAt.getTime() : plan.approvedAt,
    startedAt: plan.startedAt instanceof Date ? plan.startedAt.getTime() : plan.startedAt,
    completedAt: plan.completedAt instanceof Date ? plan.completedAt.getTime() : plan.completedAt,
    flowUpdatedAt:
      plan.flowUpdatedAt instanceof Date ? plan.flowUpdatedAt.getTime() : plan.flowUpdatedAt,
    breakStartedAt:
      plan.breakStartedAt instanceof Date ? plan.breakStartedAt.getTime() : plan.breakStartedAt,
  }
}

/**
 * GET /api/curriculum/[playerId]/sessions/plans/[planId]
 * Get a specific session plan
 */
export const GET = withAuth(async (_request, { params }) => {
  const { planId } = (await params) as { playerId: string; planId: string }

  try {
    const plan = await getSessionPlan(planId)
    if (!plan) {
      return NextResponse.json({ error: 'Plan not found' }, { status: 404 })
    }
    return NextResponse.json({ plan: serializePlan(plan) })
  } catch (error) {
    console.error('Error fetching plan:', error)
    return NextResponse.json({ error: 'Failed to fetch plan' }, { status: 500 })
  }
})

/**
 * PATCH /api/curriculum/[playerId]/sessions/plans/[planId]
 * Update session plan status or record results
 *
 * Body:
 * - action: 'approve' | 'start' | 'record' | 'record_redo' | 'end_early' | 'abandon'
 *          | 'set_remote_camera' | 'part_transition_complete' | 'break_finished' | 'break_results_acked'
 * - result?: SlotResult (for 'record' action)
 * - reason?: string (for 'end_early' action)
 */
export const PATCH = withAuth(async (request, { params }) => {
  const { playerId, planId } = (await params) as { playerId: string; planId: string }

  try {
    // Authorization: require 'start-session' permission (parent or teacher-present)
    const userId = await getUserId()
    const canModify = await canPerformAction(userId, playerId, 'start-session')
    if (!canModify) {
      return NextResponse.json({ error: 'Not authorized' }, { status: 403 })
    }

    const body = await request.json()
    const {
      action,
      result,
      reason,
      redoContext,
      remoteCameraSessionId,
      breakFinishReason,
      breakResults,
    } = body

    let plan
    const actionStart = Date.now()

    if (action === 'record') {
      console.log('[GBTRACE][server]', 'patch-record-request', {
        ts: new Date().toISOString(),
        playerId,
        planId,
        slotIndex: result?.slotIndex,
        isCorrect: result?.isCorrect,
      })
    } else if (action === 'record_redo') {
      console.log('[GBTRACE][server]', 'patch-record-redo-request', {
        ts: new Date().toISOString(),
        playerId,
        planId,
        slotIndex: result?.slotIndex,
        isCorrect: result?.isCorrect,
        redoContext,
      })
    } else if (
      action === 'part_transition_complete' ||
      action === 'break_finished' ||
      action === 'break_results_acked'
    ) {
      console.log('[GBTRACE][server]', 'patch-flow-request', {
        ts: new Date().toISOString(),
        playerId,
        planId,
        action,
        breakFinishReason: breakFinishReason ?? null,
      })
    }

    switch (action) {
      case 'approve':
        plan = await approveSessionPlan(planId)
        break

      case 'start':
        plan = await startSessionPlan(planId)
        // Emit session events to player channel (parents) and classroom channel (if present)
        await emitSessionEvents(playerId, planId, 'start')
        break

      case 'record':
        if (!result) {
          return NextResponse.json(
            { error: 'result is required for record action' },
            { status: 400 }
          )
        }
        plan = await recordSlotResult(planId, result as Omit<SlotResult, 'timestamp'>)
        break

      case 'record_redo':
        if (!result || !redoContext) {
          return NextResponse.json(
            {
              error: 'result and redoContext are required for record_redo action',
            },
            { status: 400 }
          )
        }
        plan = await recordRedoResult(
          planId,
          result as Omit<SlotResult, 'timestamp' | 'partNumber'>,
          redoContext as RedoContext
        )
        break

      case 'end_early':
        plan = await completeSessionPlanEarly(planId, reason)
        // Emit session events to player channel (parents) and classroom channel (if present)
        await emitSessionEvents(playerId, planId, 'end_early')
        break

      case 'abandon':
        plan = await abandonSessionPlan(planId)
        // Emit session events to player channel (parents) and classroom channel (if present)
        await emitSessionEvents(playerId, planId, 'abandon')
        break

      case 'set_remote_camera':
        // remoteCameraSessionId can be string (to set) or null (to clear)
        if (remoteCameraSessionId === undefined) {
          return NextResponse.json(
            {
              error: 'remoteCameraSessionId is required for set_remote_camera action',
            },
            { status: 400 }
          )
        }
        plan = await updateSessionPlanRemoteCamera(planId, remoteCameraSessionId)
        break

      case 'part_transition_complete':
        plan = await completePartTransition(planId)
        break

      case 'break_finished':
        if (
          breakFinishReason !== 'timeout' &&
          breakFinishReason !== 'gameFinished' &&
          breakFinishReason !== 'skipped'
        ) {
          return NextResponse.json(
            {
              error: 'breakFinishReason is required and must be timeout | gameFinished | skipped',
            },
            { status: 400 }
          )
        }
        plan = await finishGameBreak(planId, breakFinishReason, breakResults ?? null)
        break

      case 'break_results_acked':
        plan = await acknowledgeGameBreakResults(planId)
        break

      default:
        return NextResponse.json(
          {
            error:
              'Invalid action. Must be: approve, start, record, record_redo, end_early, abandon, set_remote_camera, part_transition_complete, break_finished, or break_results_acked',
          },
          { status: 400 }
        )
    }

    if (action === 'record' || action === 'record_redo') {
      console.log('[GBTRACE][server]', 'patch-record-response', {
        ts: new Date().toISOString(),
        playerId,
        planId,
        action,
        durationMs: Date.now() - actionStart,
        updatedPartIndex: plan.currentPartIndex,
        updatedSlotIndex: plan.currentSlotIndex,
        updatedStatus: plan.status,
        completedAt: plan.completedAt ? String(plan.completedAt) : null,
      })
    } else if (
      action === 'part_transition_complete' ||
      action === 'break_finished' ||
      action === 'break_results_acked'
    ) {
      console.log('[GBTRACE][server]', 'patch-flow-response', {
        ts: new Date().toISOString(),
        playerId,
        planId,
        action,
        updatedFlowState: plan.flowState ?? null,
        updatedBreakStartedAt: plan.breakStartedAt ? String(plan.breakStartedAt) : null,
        updatedBreakReason: plan.breakReason ?? null,
      })
    }
    return NextResponse.json({ plan: serializePlan(plan) })
  } catch (error) {
    if (error instanceof InvalidFlowTransitionError) {
      return NextResponse.json(
        {
          error: error.message,
          code: 'INVALID_FLOW_TRANSITION',
          flowState: error.state,
          eventType: error.eventType,
        },
        { status: 409 }
      )
    }
    if (error instanceof StaleFlowVersionError) {
      return NextResponse.json(
        {
          error: error.message,
          code: 'STALE_FLOW_VERSION',
          expectedFlowVersion: error.expectedFlowVersion,
          actualFlowVersion: error.actualFlowVersion,
        },
        { status: 409 }
      )
    }
    console.error('Error updating plan:', error)
    return NextResponse.json({ error: 'Failed to update plan' }, { status: 500 })
  }
})

/**
 * Helper to emit session socket events to both:
 * 1. Player channel (for parent observation) - ALWAYS
 * 2. Classroom channel (for teacher observation) - only if student is present
 */
async function emitSessionEvents(
  playerId: string,
  sessionId: string,
  action: 'start' | 'end_early' | 'abandon'
): Promise<void> {
  try {
    // Get player name
    const player = await db.query.players.findFirst({
      where: eq(players.id, playerId),
    })
    const playerName = player?.name ?? 'Unknown'

    // Always emit to player channel (for parents)
    if (action === 'start') {
      await emitSessionStartedToPlayer({ sessionId, playerId, playerName })
    } else {
      const reason = action === 'end_early' ? 'ended_early' : 'abandoned'
      await emitSessionEndedToPlayer({
        sessionId,
        playerId,
        playerName,
        reason,
      })
    }

    // Also emit to classroom channel if student is present
    const presence = await getStudentPresence(playerId)
    if (presence) {
      const classroomId = presence.classroomId
      if (action === 'start') {
        await emitSessionStarted({ sessionId, playerId, playerName }, classroomId)
      } else {
        const reason = action === 'end_early' ? 'ended_early' : 'abandoned'
        await emitSessionEnded({ sessionId, playerId, playerName, reason }, classroomId)
      }
    }

    // Fire-and-forget: notify subscribers (web push, email, in-app)
    if (action === 'start') {
      const baseUrl =
        process.env.NEXTAUTH_URL ?? process.env.NEXT_PUBLIC_APP_URL ?? 'https://abaci.one'

      import('@/lib/notifications/bootstrap')
        .then(({ bootstrapChannels }) => bootstrapChannels())
        .then(() => import('@/lib/notifications/dispatcher'))
        .then(({ notifySubscribers }) =>
          notifySubscribers({
            sessionId,
            playerId,
            playerName,
            playerEmoji: player?.emoji ?? '',
            observeUrl: `${baseUrl}/practice/${playerId}/observe`,
          })
        )
        .catch((err) => {
          console.error('[Notifications] Failed to notify subscribers:', err)
        })
    }
  } catch (error) {
    // Don't fail the request if socket emission fails
    console.error('[SessionPlan] Failed to emit session event:', error)
  }
}